package weaver.interfaces.shds.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import weaver.conn.RecordSet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title
 * @Author wangchaofa
 * @CreateDate 2023/9/8
 * @Version 1.0
 * @Description
 */
public class HttpUtils {

    /**
     *
     * @param url
     * @param headers
     * @param stringJson
     * @param encode
     * @return
     */
    public static Map<String,Object> getAppToken(String url, Map<String,String> headers, String stringJson, String encode){
        Map<String,Object> result = new HashMap<>();
        if(encode == null){
            encode = "utf-8";
        }
        RecordSet rs = new RecordSet();
        String content = null;
        CloseableHttpResponse httpResponse = null;
        CloseableHttpClient closeableHttpClient = null;
        try {
            closeableHttpClient = HttpClients.createDefault();
            HttpPost httpost = new HttpPost(url);
            //设置header
            httpost.setHeader("Content-type", "application/json");
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpost.setHeader(entry.getKey(),entry.getValue());
                }
            }
            //组织请求参数
            StringEntity stringEntity = new StringEntity(stringJson, encode);
            httpost.setEntity(stringEntity);
            //响应信息
            httpResponse = closeableHttpClient.execute(httpost);
            if(httpResponse != null){
                HttpEntity entity = httpResponse.getEntity();
                content = EntityUtils.toString(entity, encode);
                if(content != null && !"".equals(content)){
                    JSONObject json = JSONObject.parseObject(content);
                    rs.writeLog("=============  getAppToken  json ===================  "+ json);
                    String code = json.getString("state");
                    if("success".equals(code)){
                        JSONObject json2 = JSONObject.parseObject(json.getString("data"));
                        String token = json2.getString("app_token");
                        String expire = json2.getString("expire_time");

                        result.put("app_token",token);
                        result.put("expire_time",expire);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            rs.writeLog(e);
        }finally{
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {  //关闭连接、释放资源
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     *
     * @param url
     * @param headers
     * @param stringJson
     * @param encode
     * @return
     */
    public static Map<String,Object> getAccessToken(String url, Map<String,String> headers, String stringJson, String encode){
        Map<String,Object> result = new HashMap<>();
        if(encode == null){
            encode = "utf-8";
        }
        RecordSet rs = new RecordSet();
        String content = null;
        CloseableHttpResponse httpResponse = null;
        CloseableHttpClient closeableHttpClient = null;
        try {
            closeableHttpClient = HttpClients.createDefault();
            HttpPost httpost = new HttpPost(url);
            //设置header
            httpost.setHeader("Content-type", "application/json");
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpost.setHeader(entry.getKey(),entry.getValue());
                }
            }
            //组织请求参数
            StringEntity stringEntity = new StringEntity(stringJson, encode);
            httpost.setEntity(stringEntity);
            //响应信息
            httpResponse = closeableHttpClient.execute(httpost);
            if(httpResponse != null){
                HttpEntity entity = httpResponse.getEntity();
                content = EntityUtils.toString(entity, encode);
                if(content != null && !"".equals(content)){
                    JSONObject json = JSONObject.parseObject(content);
                    rs.writeLog("=============  getAccessToken  json ===================  "+ json);
                    String code = json.getString("state");
                    if("success".equals(code)){
                        JSONObject json1 = JSONObject.parseObject(json.toJSONString());
                        JSONObject json2 = JSONObject.parseObject(json1.getString("data"));
                        String token = json2.getString("access_token");
                        String expire = json2.getString("expire_time");

                        result.put("access_token",token);
                        result.put("expire_time",expire);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            rs.writeLog(e);
        }finally{
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {  //关闭连接、释放资源
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 保存
     * @param url
     * @param headers
     * @param stringJson
     * @param encode
     * @return
     */
    public static String pushInfoSave(String url, Map<String,String> headers, String stringJson, String encode){
        String result = "";
        if(encode == null){
            encode = "utf-8";
        }
        RecordSet rs = new RecordSet();
        String content = null;
        CloseableHttpResponse httpResponse = null;
        CloseableHttpClient closeableHttpClient = null;
        try {
            closeableHttpClient = HttpClients.createDefault();
            HttpPost httpost = new HttpPost(url);
            //设置header
            httpost.setHeader("Content-type", "application/json");
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpost.setHeader(entry.getKey(),entry.getValue());
                }
            }
            //组织请求参数
            StringEntity stringEntity = new StringEntity(stringJson, encode);
            httpost.setEntity(stringEntity);
            //响应信息
            httpResponse = closeableHttpClient.execute(httpost);
            if(httpResponse != null) {
                HttpEntity entity = httpResponse.getEntity();
                content = EntityUtils.toString(entity, encode);
                if (content != null && !"".equals(content)) {
                    JSONObject json = JSONObject.parseObject(content);
                    rs.writeLog("=============  pushInfo  json ===================  " + json);
                    boolean success = Boolean.parseBoolean(json.getString("status"));
                    if (success) {
                        String dataString = json.getString("data").toString();
                        JSONArray jsonArray = JSONArray.parseArray(dataString);
                        if (jsonArray.size() > 0) {
                            String data = jsonArray.get(0).toString();
                            JSONObject json1 = JSONObject.parseObject(data);
                            result = json1.getString("id").toString();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            rs.writeLog(e);
        }finally{
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {  //关闭连接、释放资源
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 提交
     * @param url
     * @param headers
     * @param stringJson
     * @param encode
     * @return
     */
    public static boolean pushInfoSubmit(String url, Map<String,String> headers, String stringJson, String encode){
        boolean result = false;
        if(encode == null){
            encode = "utf-8";
        }
        RecordSet rs = new RecordSet();
        String content = null;
        CloseableHttpResponse httpResponse = null;
        CloseableHttpClient closeableHttpClient = null;
        try {
            closeableHttpClient = HttpClients.createDefault();
            HttpPost httpost = new HttpPost(url);
            //设置header
            httpost.setHeader("Content-type", "application/json");
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpost.setHeader(entry.getKey(),entry.getValue());
                }
            }
            //组织请求参数
            StringEntity stringEntity = new StringEntity(stringJson, encode);
            httpost.setEntity(stringEntity);
            //响应信息
            httpResponse = closeableHttpClient.execute(httpost);
            if(httpResponse != null) {
                HttpEntity entity = httpResponse.getEntity();
                content = EntityUtils.toString(entity, encode);
                if (content != null && !"".equals(content)) {
                    JSONObject json = JSONObject.parseObject(content);
                    rs.writeLog("=============  pushInfo  json ===================  " + json);
                    boolean success = Boolean.parseBoolean(json.getString("status"));
                    if (success) {
                        result = true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            rs.writeLog(e);
        }finally{
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {  //关闭连接、释放资源
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 审核
     * @param url
     * @param headers
     * @param stringJson
     * @param encode
     * @return
     */
    public static boolean pushInfoAudit(String url, Map<String,String> headers, String stringJson, String encode){
        boolean result = false;
        if(encode == null){
            encode = "utf-8";
        }
        RecordSet rs = new RecordSet();
        String content = null;
        CloseableHttpResponse httpResponse = null;
        CloseableHttpClient closeableHttpClient = null;
        try {
            closeableHttpClient = HttpClients.createDefault();
            HttpPost httpost = new HttpPost(url);
            //设置header
            httpost.setHeader("Content-type", "application/json");
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpost.setHeader(entry.getKey(),entry.getValue());
                }
            }
            //组织请求参数
            StringEntity stringEntity = new StringEntity(stringJson, encode);
            httpost.setEntity(stringEntity);
            //响应信息
            httpResponse = closeableHttpClient.execute(httpost);
            if(httpResponse != null) {
                HttpEntity entity = httpResponse.getEntity();
                content = EntityUtils.toString(entity, encode);
                if (content != null && !"".equals(content)) {
                    JSONObject json = JSONObject.parseObject(content);
                    rs.writeLog("=============  pushInfo  json ===================  " + json);
                    boolean success = Boolean.parseBoolean(json.getString("status"));
                    if (success) {
                        result = true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            rs.writeLog(e);
        }finally{
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {  //关闭连接、释放资源
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

}
